<?php

class Tag extends BaseOrm {
	public $Type = 'Tag';
	//protected $_hasMany = array('SmileyTag' => 'id');
	//protected $_belongsTo = array('User' => 'usr_id', 'Blog' => 'blog_id', 'Post' => 'post_id');

	public function __construct($params) {
		if ( count($params) == 1 && isset($params['dname']) ) {
			$this->SetValuesByDname($params['dname']);
		} elseif ( is_array($params) ) {
			$this->Values = $params;
		} elseif ( intval($params) ) {
			$this->SetValuesByPrimaryKey(intval($params));
		}
	}

	public function Id() {
		return $this->Values['id'];
	}

	public function Name() {
		return htmlspecialchars($this->Values['dname']);
	}

	private $_img;
	private $_checked_img = false;
	public function Img() {
		if ( $this->_img || $this->_checked_img ) return $this->_img;
		$this->_checked_img = true;
		$img = array();

		$path = Monkake::Get('images_directory') . $this->Values['dimage'];
		if ( file_exists($path) ) {
			list($img['img_width'], $img['img_height'], $attr) = getimagesize($path);
			$img['img_src'] = Site::GetMediaUrl('images/' . $this->Values['dimage']);
			$this->_img = $img;
		}

		return $this->_img;
	}

	public function ImgSrc() {
		return Monkake::Get('images_directory') . $this->Values['dimage'];
	}

	public function Url() {
		return '/smileys/' . ucfirst($this->Values['dname']);
	}

	public function Count() {
		return $this->Values['tag_count'];
	}

	public function Voted() {
		return @$this->Values['voted'];
	}

	public function UpUrl() {
		return Site::GetUrl('set_tag/', 'id=' . $this->Values['id'] . '&back_url=' . urlencode($this->Url()) . '&action=add_tag&tag=' . urlencode($this->Name()));
	}

	public function DownUrl() {
		return Site::GetUrl('set_tag/', 'id=' . $this->Values['id'] . '&back_url=' . urlencode($this->Url()) . '&action=remove_tag&tag=' . urlencode($this->Name()));
	}

	public function AddVote($user) {
	
	}

	public function RemoveVote($user) {
		
	}

	private $_smileys;
	public function Smileys($offset, $limit) {
		if ( !$this->_smileys ) $this->_smileys = new SmileyList($offset, $limit, $this);
		return $this->_smileys;
	}

	// STATIC
	public function SetValuesByDname($dname) {
		$query = "
			/* " . __FILE__ . "::" . __LINE__ . " */
			SELECT
				" . $this->EssentialColumns() . "
			FROM
				" . $this->TableName() . " AS " . $this->TableAlias() . "
			WHERE
				" . $this->TableAlias() . ".dname='" . mysql_escape_string($dname) . "'
			LIMIT
				1
		";

		$result = Dbi::GetResult($query);

		// TODO: exception
		if ( !$result || !mysql_num_rows($result) ) return false;

		$row = mysql_fetch_assoc($result);

		$this->Values = $row;
		return true;
	}
}

class TagList extends BaseOrmList {
	public $Type = 'Tag';

	public function __construct($offset = 0, $limit = 10, $parent = NULL) {
		if ( !$parent ) {
		} elseif ( $parent->Type == 'Smiley' ) {
			$this->SetValuesBySmiley($parent, $offset, $limit);
		} elseif ( is_object($parent) ) {
			$this->SetValuesByParent($parent, $offset, $limit);
		}
	}

	public function SetValuesForTopTags($offset = 0, $limit = 10) {
		$query = "
			/* " . __FILE__ . "::" . __LINE__ . " */
			SELECT
				COUNT(*) AS count,
				g.dname AS dname,
				g.dimage
			FROM
				msnu_smileysgenre AS sg
			LEFT OUTER JOIN
				msnu_sgenre AS g ON
					sg.sgenre_id=g.id
			GROUP BY
				g.id
			ORDER BY
				count DESC
			LIMIT
				32
			";

		$this->_set($offset, $limit, $query);
	}

	public function SetValuesBySmiley($parent, $offset, $limit) {
		$smiley_id = $parent->Id();
		$essential_columns = BaseOrm::GetEssentialColumns($this->Type);
		$table_alias = BaseOrm::GetTableAlias($this->Type);

		$user_id = Site::$UserId;

		// TODO get tag count for smiley
		$count_query = "
			/* " . __FILE__ . "::" . __LINE__ . " */
			SELECT
				COUNT(*) AS c
			FROM
				msnu_smileysgenre AS f
			WHERE
				f.smiley_id=" . $smiley_id . "
			";

		$query = "
			/* " . __FILE__ . "::" . __LINE__ . " */
			SELECT
				" . $essential_columns . ",
				COUNT(s.usr_id) AS tag_count" . (($user_id)?",
				ut.usr_id AS voted":"") . "
			FROM
				msnu_smileysgenre AS s
			LEFT OUTER JOIN
				msnu_sgenre AS $table_alias ON
					$table_alias.id=s.sgenre_id" . (($user_id)?"
			LEFT OUTER JOIN
				msnu_smileysgenre AS ut ON
					ut.smiley_id=s.smiley_id AND
					ut.sgenre_id=s.sgenre_id AND
					ut.enabled=1 AND
					ut.usr_id=1":"") . "
			WHERE
				s.smiley_id = " . $smiley_id . "
			GROUP BY
				s.sgenre_id
			ORDER BY
				tag_count DESC
			";

		$this->_set($offset, $limit, $query, $count_query);
	}
}